package servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import pojo.Customer;
import service.CustomerManager;
import service.impl.CustomerManagerFactory;

/**
 * Servlet implementation class UpdateCustomerServlet
 */
public class UpdateCustomerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private CustomerManager manager;   
    
    public UpdateCustomerServlet() {
        super();
        this.manager = CustomerManagerFactory.getInstance();
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		///接参
		String curUpdateId = request.getParameter("id");
		
		
		///校验
		//首先，如果客戶端傳來的cid ，和服務端的curUpdateId不匹配，那么，不能修改，重定向到error.jsp
		Customer c = this.manager.searchCustomer(Integer.parseInt(curUpdateId));
		if(c!=null){
			request.setAttribute("c", c);
			request.getRequestDispatcher("update.jsp").forward(request, response);
		}else{
			//失败
			//重定向
			request.setAttribute("msg", "update error！");
			try {
				request.getRequestDispatcher("/ErrorMsgServlet").forward(request, response);
			} catch (ServletException | IOException e) {
				e.printStackTrace();
			}
		}
	}

}
